import { message } from 'antd';
import { history } from 'umi';
import { addVipCard,updateVipCard,fetchVipCard,fetchShopList } from './service';

const Model = {
  namespace: 'cubeVipCard',
  state: {
    shopList:[],
    shopOptionList:[],
    vipCard:{},
    cloneVipCard:{}
  },
  effects: {
    *add({ payload }, { call }) {
      let result=yield call(addVipCard, payload);
      if(result.code===0){
        message.success('提交成功');
        history.push(`/vip/card`)
      }else{
        message.error(result.message);
      }
    },
    *update({ payload }, { call }) {
      let result=yield call(updateVipCard, payload);
      if(result.code===0){
        message.success('编辑成功');
        history.push(`/vip/card`)
      }else{
        message.error(result.message);
      }
    },
    *fetchShopList({ payload }, { call,put }) {
      let result=yield call(fetchShopList, payload);
      if(result.code===0){
        let _shopOptionList=[]
        if(result.data){
          result.data.map((item)=>{
            _shopOptionList.push({
              label:item.name,
              value:item.id
            })
          })
        }
        yield put({
          type: 'common',
          payload: {
            shopList:result.data,
            shopOptionList:_shopOptionList
          }
        });
      }else{
        message.error(result.message);
      }
    },
    *fetchVipCard({ payload }, { call,put }) {
      let result=yield call(fetchVipCard, payload);
      if(result.code===0){
        yield put({
          type: 'common',
          payload: {
            vipCard:result.data,
            cloneVipCard:result.data,
          }
        });
        return result.data
      }else{
        message.error(result.message);
      }
    },
  },
  reducers: {
    common(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};
export default Model;
